package servlet;

import java.io.IOException;
import model.City;
import model.State;
import model.Country;
import model.Preference;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.User;
import model.Address;
/**
 * Servlet implementation class SignUp
 */
@WebServlet("/SignUp")
public class Registration extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Registration() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String salutation = "" , firstname = "" , middlename = "" , lastname = "" , displayname = "" , dateofbirth = "" , sex = "" , ssn = "" , add1 = "";
		String add2 = "" , add3 = "" , phone = "" , cityname = "" , statename = "" , countryname = "" , zip = "" , loginid = "" , password = "" , emailid = "" ;
		String cardONE = "" , cardTWO = "" , cardTHREE = "" , cardFOUR = "" , cardNAME = "" , cardCVV = "" , cardEXPDT = "" , preferenceType = "";
		
		User user = new User();
		Address address = new Address();
		City city = new City();
		State state = new State();
		Country country = new Country();
		Preference preference = new Preference();
		try{
			if(!(request.getParameter("salutation").equals("--[Select One]--")))
				user.setSalutation(request.getParameter("salutation"));
			
			
			if(!(request.getParameter("first_name") == null))
				user.setFirstName(request.getParameter("first_name"));
			
			if(!((request.getParameter("middle_name") == null) || (request.getParameter("middle_name").isEmpty())))
				user.setMiddleName(request.getParameter("middle_name"));
			
			if(!(request.getParameter("last_name") == null))
				user.setLastName(request.getParameter("last_name"));
			
			if(!(request.getParameter("display_name") == null))
				user.setDisplayName(request.getParameter("display_name"));

			if(!(request.getParameter("SnapHost_Calendar") == null))
				user.setDob(request.getParameter("SnapHost_Calendar"));
			
			if(!(request.getParameter("sex") == null))
				user.setSex(request.getParameter("sex"));
					
			if(!(request.getParameter("add_1") == null))
				address.setAddrLine1(request.getParameter("add_1"));
				
			
			if(!(request.getParameter("add_2") == null))
				address.setAddrLine2(request.getParameter("add_2"));
				
			
			if(!(request.getParameter("add_3") == null))
				address.setAddrLine3(request.getParameter("add_3"));
			
			if(!(request.getParameter("contact_num") == null))
				address.setTelNo(request.getParameter("contact_num"));
			
			if(!((request.getParameter("city") == null) || (request.getParameter("city").isEmpty())))
				city.setCityName(request.getParameter("city"));
				address.setCity(city);
				
			if(!((request.getParameter("state") == null) || (request.getParameter("state").isEmpty())))
				state.setStateName(request.getParameter("state"));
				address.setState(state);
				
			if(!((request.getParameter("country") == null) || (request.getParameter("country").isEmpty())))
				country.setCountryName(request.getParameter("country"));
				address.setCountry(country);
				
			if(!((request.getParameter("zipcode") == null) || (request.getParameter("zipcode").isEmpty())))
				address.setZipCode(Integer.parseInt(request.getParameter("zipcode")));
			
			if(!(request.getParameter("login_id") == null))
				user.setLoginId(request.getParameter("login_id"));
			
			if(!(request.getParameter("email_id") == null))
				user.setEmailId(request.getParameter("email_id"));
			
			if(!(request.getParameter("password") == null))
				user.setPassword(request.getParameter("password"));
			
			if(!(request.getParameter("userType") == null))
				preference.setPreferenceType(request.getParameter("userType"));
			user.setPreference(preference);
			user.setAddress(address);
			
			services.ServiceProxy proxy = new services.ServiceProxy();
			proxy.setEndpoint("http://localhost:8080/videolibrary/services/Service");
			
			if(!(preference.getPreferenceType() == null)){
				if(preference.getPreferenceType().equals("Simple User")){
					String stateString = proxy.signUp(user);
					if(stateString == null) stateString = "false Invalid entry";
					System.out.println("TST1");
					System.out.println(stateString.isEmpty());
					if(stateString.contains("false")){
						String st = stateString.substring(5);
						st = st.replace(" Invalid", "\\nInvalid");
						System.out.println(st);
						RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/LibraryJSP/Registration.jsp?result="+st);
						request.setAttribute("result", st);
						dispatcher.forward(request, response);
					}else{
						RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/LibraryJSP/Login.jsp?result="+stateString);
						request.setAttribute("result", stateString);
						dispatcher.forward(request, response);
					}
				}
				if(preference.getPreferenceType().equals("Premium User")){
						if((request.getParameter("ssn1").isEmpty()) || (request.getParameter("ssn1") == null) || 
								(request.getParameter("ssn2").isEmpty()) || (request.getParameter("ssn2") == null) || 
								(request.getParameter("ssn3").isEmpty()) || (request.getParameter("ssn3") == null))ssn.concat(" ");
						else ssn = ssn.concat(request.getParameter("ssn1")).concat("-").concat(request.getParameter("ssn2")).concat("-").concat(request.getParameter("ssn3"));
						if((request.getParameter("card_one").isEmpty()) || (request.getParameter("card_one") == null))cardONE = " ";
						else cardONE = request.getParameter("card_one");
						if((request.getParameter("card_two").isEmpty()) || (request.getParameter("card_two") == null))cardTWO = " ";
						else cardTWO = request.getParameter("card_two");
						if((request.getParameter("card_three").isEmpty()) || (request.getParameter("card_three") == null))cardTHREE = " ";
						else cardTHREE = request.getParameter("card_three");
						if((request.getParameter("card_four").isEmpty()) || (request.getParameter("card_four") == null))cardFOUR = " ";
						else cardFOUR = request.getParameter("card_four");
						if((request.getParameter("card_name").isEmpty()) || (request.getParameter("card_name") == null))cardNAME = " ";
						else cardNAME = request.getParameter("card_name");
						if((request.getParameter("card_cvv").isEmpty()) || (request.getParameter("card_cvv") == null))cardCVV = " ";
						else cardCVV = request.getParameter("card_cvv");
						if((request.getParameter("card_expdate").isEmpty()) || (request.getParameter("card_expdate") == null))cardEXPDT = " ";
						else cardEXPDT = request.getParameter("card_expdate");
						String stateString = "true";
						if(stateString.contains("true")){
							stateString = proxy.signUp(user);
							if(stateString.contains("false")){
								String st = stateString.substring(5);
								st = st.replace(" Invalid", "\\nInvalid");
								RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/LibraryJSP/Registration.jsp?result="+st);
								request.setAttribute("result", st);
								dispatcher.forward(request, response);
							}else{
								RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/LibraryJSP/Login.jsp?result="+stateString);
								request.setAttribute("result", stateString);
								dispatcher.forward(request, response);
							}
						}else{
							String st = stateString.substring(5);
							st = st.replace(" Invalid", "\\nInvalid");
							RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/LibraryJSP/Registration.jsp?result="+st);
							request.setAttribute("result", st);
							dispatcher.forward(request, response);
						}
				}
			}else 
				{String res= proxy.signUp(user);
				String st = res.substring(5);
				st = st.replace(" Invalid", "\\nInvalid");
				System.out.println(st);
				RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/LibraryJSP/Registration.jsp?result="+st);
				request.setAttribute("result", st);
				dispatcher.forward(request, response);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static String[] country(){
		String[] country = new String[110];
		try{
			services.ServiceProxy proxy = new services.ServiceProxy();
			proxy.setEndpoint("http://localhost:8080/videolibrary/services/Service");

			country = proxy.listCountry();
		}catch(Exception e){
			e.printStackTrace();
		}
		return country;
	}
	
	public static String[] state(String country){
		String[] state = new String[110];
		try{
			services.ServiceProxy proxy = new services.ServiceProxy();
			proxy.setEndpoint("http://localhost:8080/videolibrary/services/Service");

			state = proxy.listState(country);
		}catch(Exception e){
			e.printStackTrace();
		}
		return state;
	}
	
	public static String[] city(String state){
		String[] city = new String[110];
		try{
			services.ServiceProxy proxy = new services.ServiceProxy();
			proxy.setEndpoint("http://localhost:8080/videolibrary/services/Service");

			city = proxy.listCity(state);
		}catch(Exception e){
			e.printStackTrace();
		}
		return city;
	}

}
